// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids;

import org.apache.doris.common.AnalysisException;
import org.apache.doris.nereids.jobs.cascades.OptimizeGroupJob;
import org.apache.doris.nereids.jobs.scheduler.JobStack;
import org.apache.doris.nereids.jobs.scheduler.SimpleJobScheduler;
import org.apache.doris.nereids.memo.Group;
import org.apache.doris.nereids.memo.Memo;
import org.apache.doris.nereids.properties.PhysicalProperties;
import org.apache.doris.nereids.rules.RuleSet;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
import org.apache.doris.nereids.trees.plans.physical.PhysicalPlan;
import org.apache.doris.qe.ConnectContext;

/**
 * Planner to do query plan in Nereids.
 */
public class Planner {
    private PlannerContext plannerContext;

    /**
     * Do analyze and optimize for query plan.
     *
     * @param plan wait for plan
     * @param outputProperties physical properties constraints
     * @param connectContext connect context for this query
     * @return physical plan generated by this planner
     * @throws AnalysisException throw exception if failed in ant stage
     */
    // TODO: refactor, just demo code here
    public PhysicalPlan plan(
            LogicalPlan plan,
            PhysicalProperties outputProperties,
            ConnectContext connectContext) throws AnalysisException {
        OptimizerContext optimizerContext = new OptimizerContext(
                new Memo(),
                new RuleSet(),
                new JobStack(),
                new SimpleJobScheduler()
        );
        plannerContext = new PlannerContext(optimizerContext, connectContext, outputProperties);
        plannerContext.getOptimizerContext().getMemo().initialize(plan);
        plannerContext.getOptimizerContext().pushTask(new OptimizeGroupJob(getRoot(), plannerContext));
        plannerContext.getOptimizerContext().getJobScheduler().executeJobStack(plannerContext);
        return getBestPlan();
    }

    public Group getRoot() {
        return plannerContext.getOptimizerContext().getMemo().getRootSet();
    }

    private PhysicalPlan getBestPlan() {
        return null;
    }
}
